﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using Kaidou.Util;
using Kaidou.Bll;
using System.Collections.Generic;
using System.Globalization;
using System.Text;


public partial class seal_seal_report_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];

        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }

        if (loginModel.isKumiaiin())
        {
            Response.Redirect("../menu.aspx");
        }

        if (loginModel.isKumiaiin() && sessioncontrol.control())
        {
            Response.Redirect("../control.aspx");
        }

        try
        {
            if (!IsPostBack)
            {
                string report_no = Request.QueryString["report_no"].ToString();
                TRN_Report_D_Persister dao1 = new TRN_Report_D_Persister();
                TRN_Report_H_Persister dao2 = new TRN_Report_H_Persister();
                MST_TraderPersister dao3 = new MST_TraderPersister();
                MST_UnionPersister dao4 = new MST_UnionPersister();
                MST_Union_BranchPersister dao5 = new MST_Union_BranchPersister();
                TRN_ReportManager manager = new TRN_ReportManager();

                Report rpt = new Report();

                ArrayList reports = dao1.get(report_no);
                TrnReporth model = dao2.getDetail(report_no);
                TrnReportd[] model_D = new TrnReportd[5];
                int i = 0;
               
                for (i = 0; i < 5; i++)
                {
                    model_D[i] = new TrnReportd();
                }
               
                for (i = 0; i < reports.Count; i++)
                {
                    
                    model_D[((TrnReportd)reports[i]).Id.ReportRecNo - 1] = (TrnReportd)reports[i];

                }
                rpt.setReportH(model);
                rpt.setReportD(model_D);

                int alertCount = 0;
                int alertOrderUnion = 0;
                alertCount = manager.countDuplicationReportForUpdate(rpt);
                alertOrderUnion = manager.countDifferentUnion(rpt, model.UnionCode);

                //支店名取得
                mst_union_branch branch = dao5.getDetail(model.UnionCode, model.BranchCode);
                MstUnion union = dao4.getDetail(model.UnionCode);
                MstTrader trader= dao3.getDetail(model.TraderRegNo);

                if (alertOrderUnion != 0 || alertCount != 0)
                {
                    alertPanel.Visible = true;
                    string labelmsg = "";
                    if (alertCount != 0)
                    {
                        labelmsg = labelmsg + "※既にシールは報告済みです。<br>";
                    }
                    if (alertOrderUnion != 0)
                    {
                        labelmsg = labelmsg + "※シール購入者と報告者が異なります。";
                    }
                    alert.Text = labelmsg;
                }

                if (TrnReporth.CLSS_FAX.Equals(model.ReportClss) && TrnReporth.ERROR_ON.Equals(model.FaxErrorFlg))
                {
                    faxocr_text_panel.Visible = true;
                    StringBuilder fx = new StringBuilder(); 
                    fx.Append(model.FaxocrText1 == null ? "" : ("＃" + model.FaxocrText1 + "<br>"));
                    fx.Append(model.FaxocrText2 == null ? "" : ("＃" + model.FaxocrText2 + "<br>"));
                    fx.Append(model.FaxocrText3 == null ? "" : ("＃" + model.FaxocrText3 + "<br>"));
                    fx.Append(model.FaxocrText4 == null ? "" : ("＃" + model.FaxocrText4 + "<br>"));
                    fx.Append(model.FaxocrText5 == null ? "" : ("＃" + model.FaxocrText5 + "<br>"));
                    fx.Append(model.FaxocrText6 == null ? "" : ("＃" + model.FaxocrText6 + "<br>"));
                    fx.Append(model.FaxocrText7 == null ? "" : ("＃" + model.FaxocrText7 + "<br>"));
                    fx.Append(model.FaxocrText8 == null ? "" : ("＃" + model.FaxocrText8 + "<br>"));
                    fx.Append(model.FaxocrText9 == null ? "" : ("＃" + model.FaxocrText9 + "<br>"));
                    fx.Append(model.FaxocrText10 == null ? "" : ("＃" + model.FaxocrText10 + "<br>"));
                    fx.Append(model.FaxocrText11 == null ? "" : ("＃" + model.FaxocrText11 + "<br>"));
                    fx.Append(model.FaxocrText12 == null ? "" : ("＃" + model.FaxocrText12 + "<br>"));
                    fx.Append(model.FaxocrText13 == null ? "" : ("＃" + model.FaxocrText13 + "<br>"));
                    faxocr_text.Text = fx.ToString();

                }

                REPORT_YY.Text = model.ReportDate.ToString("yy");
                REPORT_MM.Text = model.ReportDate.ToString("MM");
                REPORT_DD.Text = model.ReportDate.ToString("dd");
                if (TrnReporth.CLSS_FAX.Equals(model.ReportClss))
                {
                    fax_rcv_date.Text = "受信日：" + ((DateTime)model.FaxRcvDate).ToString("yyyy年MM月dd日");
                }
                UNION_CODE.Text = model.UnionCode;
                UNION_NAME.Text = union == null ? "" : union.OfficeName;
                BRANCH_CODE.Text = model.BranchCode == 0 ? "000" : model.BranchCode.ToString();
                BRANCH_NAME.Text = branch == null ? "本店" : branch.officename;
                TRN_JUCHU_TANTO_NAME.Text = model.ReportTantoName;
                if (TrnReporth.EST_COMP_CLSS_SELF.Equals(model.EstCompClss))
                {
                    EST_COMP_CLSS1.Checked = true;
                }
                if (TrnReporth.EST_COMP_CLSS_OTHER.Equals(model.EstCompClss))
                {
                    EST_COMP_CLSS2.Checked = true;
                }
                CONS_COMP_NAME.Text = model.ConsCompName;
                CONS_COMP_TELNO.Text = model.ConsCompTelno;
                HALL_NAME.Text = model.HallName;
                string area = Resources.Resource.area;
                string[] areaArray = area.Split(',');
                for (int j = 0; j < areaArray.Length; j++)
                {
                    ListItem item = new ListItem();
                    item.Value = (j + 1) + "";
                    if (model.HallPrefCode == (j+1))
                    {
                        item.Selected = true;
                    }
                    item.Text = areaArray[j];
                    HALL_PREF_CODE.Items.Insert(j + 1, item);
                }
                HALL_CITY.Text = model.HallCity;
                HALL_ADDRESS.Text = model.HallAddress;
                HALL_TEL.Text = model.HallTel;
                MACHINE_NAME.Text = model.MachineName;
                MACHINE_QTY.Text = model.MachineQty.ToString();
                EST_YEAR.Text = model.EstDate.ToString("yy");
                EST_MONTH.Text = model.EstDate.ToString("MM");
                EST_DAY.Text = model.EstDate.ToString("dd");
                TRADER_REG_NO1.Text = StringUtil.isEmpty( model.TraderRegNo)?"": model.TraderRegNo.Substring(0,2);
                TRADER_REG_NO2.Text = StringUtil.isEmpty(model.TraderRegNo) ? "" : model.TraderRegNo.Substring(2, 5);
                trader_label.Text = trader == null ? "" : trader.TraderName;
                CHIEF_NO1.Text = model.ChiefNo.Substring(0, 2);
                CHIEF_NO2.Text = model.ChiefNo.Substring(2,4);
                CHIEF_NAME.Text = model.ChiefName;
                WORKER1_NAME.Text = model.Worker1Name;
                WORKER2_NAME.Text = model.Worker2Name;
                WORKER3_NAME.Text = model.Worker3Name;
                WORKER4_NAME.Text = model.Worker4Name;
                WORKER5_NAME.Text = model.Worker5Name;
                WORKER6_NAME.Text = model.Worker6Name;

                

                SEAL_STR_NO1.Text = model_D[0].SealStrNo;
                SEAL_END_NO1.Text = model_D[0].SealEndNo;

                if ("1".Equals(model_D[0].MachineClss))
                {
                    MACHINE_CLSS11.Checked = true;
                }
                if ("2".Equals(model_D[0].MachineClss))
                {
                    MACHINE_CLSS12.Checked = true;
                }

                SEAL_STR_NO2.Text = model_D[1].SealStrNo;
                SEAL_END_NO2.Text = model_D[1].SealEndNo;

                if ("1".Equals(model_D[1].MachineClss))
                {
                    MACHINE_CLSS21.Checked = true;
                }
                if ("2".Equals(model_D[1].MachineClss))
                {
                    MACHINE_CLSS22.Checked = true;
                }

                SEAL_STR_NO3.Text = model_D[2].SealStrNo;
                SEAL_END_NO3.Text = model_D[2].SealEndNo;

                if ("1".Equals(model_D[2].MachineClss))
                {
                    MACHINE_CLSS31.Checked = true;
                }
                if ("2".Equals(model_D[2].MachineClss))
                {
                    MACHINE_CLSS32.Checked = true;
                }

                SEAL_STR_NO4.Text = model_D[3].SealStrNo;
                SEAL_END_NO4.Text = model_D[3].SealEndNo;

                if ("1".Equals(model_D[3].MachineClss))
                {
                    MACHINE_CLSS41.Checked = true;
                }
                if ("2".Equals(model_D[3].MachineClss))
                {
                    MACHINE_CLSS42.Checked = true;
                }

                SEAL_STR_NO5.Text = model_D[4].SealStrNo;
                SEAL_END_NO5.Text = model_D[4].SealEndNo;

                if ("1".Equals(model_D[4].MachineClss))
                {
                    MACHINE_CLSS51.Checked = true;
                }
                if ("2".Equals(model_D[4].MachineClss))
                {
                    MACHINE_CLSS52.Checked = true;
                }

                Session["report"] = rpt;
            }

        }catch(Exception exx){
            LogManager.error("seal_seal_report_detail "+exx.Message+" "+exx.StackTrace);
        }
    }

    protected void update(object sender, EventArgs e)
    {
        try
        {
            Report rpt = (Report)Session["report"];
            LoginModel loginModel = (LoginModel)Session["loginModel"];

            TrnReporth model = rpt.getReportH();
            //報告見出セット(TRN_Report_H)
            model.UnionCode = UNION_CODE.Text;
            model.BranchCode = short.Parse(BRANCH_CODE.Text);
            model.ReportTantoName = TRN_JUCHU_TANTO_NAME.Text;
            model.EstCompClss = EST_COMP_CLSS1.Checked ? "0" : EST_COMP_CLSS2.Checked ? "1" : null;
            model.ConsCompName = CONS_COMP_NAME.Text;
            model.ConsCompTelno = CONS_COMP_TELNO.Text;
            model.ConsCompTelno = CONS_COMP_TELNO.Text;
            model.HallName = HALL_NAME.Text;
            model.HallPrefCode = short.Parse(HALL_PREF_CODE.Text);
            model.HallCity = HALL_CITY.Text;
            model.HallAddress = HALL_ADDRESS.Text;
            model.HallTel = HALL_TEL.Text;
            model.MachineName = MACHINE_NAME.Text;
            model.MachineQty = MACHINE_QTY.Text.Trim().Length == 0 ? -1 : int.Parse(MACHINE_QTY.Text);
            model.EstYear = EST_YEAR.Text;
            model.EstMonth = EST_MONTH.Text;
            model.EstDay = EST_DAY.Text;
            if (StringUtil.isValidDate(EST_YEAR.Text, EST_MONTH.Text, EST_DAY.Text))
            {

                CultureInfo culture = new CultureInfo("ja-JP");
                model.EstDate = Convert.ToDateTime((int.Parse(EST_YEAR.Text) + 2000) + "/" + int.Parse(EST_MONTH.Text) + "/" + int.Parse(EST_DAY.Text), culture);

            }
            model.TraderRegNo1 = TRADER_REG_NO1.Text;
            model.TraderRegNo2 = TRADER_REG_NO2.Text;
            model.TraderRegNo = TRADER_REG_NO1.Text + TRADER_REG_NO2.Text;
            model.ChiefNo1 = CHIEF_NO1.Text;
            model.ChiefNo2 = CHIEF_NO2.Text;
            model.ChiefNo = CHIEF_NO1.Text + CHIEF_NO2.Text;
            model.ChiefName = CHIEF_NAME.Text;
            model.Worker1Name = WORKER1_NAME.Text;
            model.Worker2Name = WORKER2_NAME.Text;
            model.Worker3Name = WORKER3_NAME.Text;
            model.Worker4Name = WORKER4_NAME.Text;
            model.Worker5Name = WORKER5_NAME.Text;
            model.Worker6Name = WORKER6_NAME.Text;
            model.ReportYear = REPORT_YY.Text;
            model.ReportMonth = REPORT_MM.Text;
            model.ReportDay = REPORT_DD.Text;
            if (StringUtil.isValidDate(REPORT_YY.Text, REPORT_MM.Text, REPORT_DD.Text))
            {

                CultureInfo culture = new CultureInfo("ja-JP");
                model.ReportDate = Convert.ToDateTime((int.Parse(model.ReportYear) + 2000) + "/" + int.Parse(model.ReportMonth) + "/" + int.Parse(model.ReportDay), culture);

            }
            
            rpt.setReportH(model);

            TrnReportd[] models = rpt.getReportD();
            
            models[0].SealStrNo = SEAL_STR_NO1.Text;
            models[0].SealEndNo = SEAL_END_NO1.Text;
            models[0].MachineClss = MACHINE_CLSS11.Checked ? "1" : MACHINE_CLSS12.Checked ? "2" : null;

            models[1].SealStrNo = SEAL_STR_NO2.Text;
            models[1].SealEndNo = SEAL_END_NO2.Text;
            models[1].MachineClss = MACHINE_CLSS21.Checked ? "1" : MACHINE_CLSS22.Checked ? "2" : null;

            models[2].SealStrNo = SEAL_STR_NO3.Text;
            models[2].SealEndNo = SEAL_END_NO3.Text;
            models[2].MachineClss = MACHINE_CLSS31.Checked ? "1" : MACHINE_CLSS32.Checked ? "2" : null;

            models[3].SealStrNo = SEAL_STR_NO4.Text;
            models[3].SealEndNo = SEAL_END_NO4.Text;
            models[3].MachineClss = MACHINE_CLSS41.Checked ? "1" : MACHINE_CLSS42.Checked ? "2" : null;

            models[4].SealStrNo = SEAL_STR_NO5.Text;
            models[4].SealEndNo = SEAL_END_NO5.Text;
            models[4].MachineClss = MACHINE_CLSS51.Checked ? "1" : MACHINE_CLSS52.Checked ? "2" : null;
            
            rpt.setReportD(models);

            TRN_ReportManager validation = new TRN_ReportManager();
            List<string> error1 = validation.checkUpdateReportH(rpt.getReportH(), Resources.Resource.ResourceManager, loginModel);
            List<string> error2 = validation.checkUpdateReportD(rpt.getReportD(), Resources.Resource.ResourceManager);
            List<string> errorCodeList = error1;
            errorCodeList.AddRange(error2);

            msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

            if (errorCodeList.Count == 0)
            {
                Session["report"] = rpt;
                Response.Redirect("seal_report_update.aspx");
            }

        }
        catch (Exception e1)
        {
            LogManager.error("seal_report checckUpdateReport error" + e1.Message + e1.StackTrace);
        }
    }

    protected void delete(object sender, EventArgs e)
    {
        Response.Redirect("seal_report_delete.aspx");
    }

    protected void goBack(object sender, EventArgs e)
    {
        Session["back"] = 1;
        Response.Redirect("seal_report_search.aspx");
    }
}
